<html>
<head>

<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="content-language" content="ru-ru" />
<title>Список сотрудников (полный)</title>
<style type="text/css">
a {font-family : Arial; text-decoration : none}
a:link {color : #000000}
a:visited {color : #000000}
a:active {color : #000000}
a:hover {color : #0000ff}
</style>
</head>

<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">
<h1>Список сотрудников (полный)</h1>
<h4>[Полная версия] | <a href="directory_short.php">короткая версия</a></h4>
<u><a href=directory_csv.php>Сохранить в CSV для Outlook и Gmail</a><br/><br/></u>

<?php
// ===
// This PHP script is used to display list of Active Directory users on a webpage, grabbing it via LDAP connection.
// Evgeny Varnavsky 2009-2010
// http://code.google.com/p/aduserlist/
// LGPL license
// ===

$sort_by=$_GET['sort_by'];

ob_start();

$pagename = $_SERVER['PHP_SELF'];

include('ldap_settings_inc.php');

// OPTION
$ldapbase = "DC=spotall,DC=local";

$ldapattr = array("samaccountname", "mail", "cn", "displayname", "mobile", "telephonenumber", "description", "title", "department", "company");

// $ldapfltr = "(&(objectClass=user))|(mobile=*)(telephoneNumber=*)";
// $ldapfltr = "(&(objectClass=user)(mobile=*)(telephoneNumber=*))";
// $ldapfltr = "(&(objectCategory=person)(objectClass=user))";

// Only records where one of these attribs is not empty
$ldapfltr = "(|(mobile=*)(telephoneNumber=*)(department=*))";

// $mobilefltr = "|(mobile=*1*)(mobile=*2*)(mobile=*3*)(mobile=*4*)(mobile=*5*)(mobile=*6*)(mobile=*7*)(mobile=*8*)(mobile=*9*)(mobile=*0*)";
// $phonefltr = "|(telephoneNumber=*1*)(telephoneNumber=*2*)(telephoneNumber=*3*)(telephoneNumber=*4*)(telephoneNumber=*5*)(telephoneNumber=*6*)(telephoneNumber=*7*)(telephoneNumber=*8*)(telephoneNumber=*9*)(telephoneNumber=*0*)";
// $ldapfltr = "(&(|($mobilefltr)($phonefltr))(|(objectClass=contact)(&(objectClass=user)(useraccountcontrol=512))))";

//$ldapfltr = "(|(mobile=*1*)(mobile=*2*)(mobile=*3*)(mobile=*4*)(mobile=*5*)(mobile=*6*)(mobile=*7*)(mobile=*8*)(mobile=*9*)(mobile=*0*))";

// Search filter reference:
// http://msdn.microsoft.com/en-us/library/aa746475(VS.85).aspx
// http://support.microsoft.com/kb/555638

// Attribute reference
// Use lowercase for attribute names
// https://fsuid.fsu.edu/admin/lib/WinADLDAPAttributes.html
// http://computerperformance.co.uk/Logon/LDAP_attributes_active_directory.htm

$ldapconn = ldap_connect($ldaphost, $ldapport) or die("Could not connect to $ldaphost");

ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
ldap_set_option($ldapconn, LDAP_OPT_SIZELIMIT, 2000);

ldap_bind($ldapconn,$ldapuser,$ldappass) or die("Can not bind as $ldapuser");

$ldapsrch = ldap_search($ldapconn, $ldapbase, $ldapfltr, $ldapattr);
ldap_sort  ($ldapconn  , $ldapsrch  , "$sort_by");
$ldaprecs = ldap_get_entries($ldapconn, $ldapsrch);

echo "Пользователей получено из каталога Active Directory: <b>" . $ldaprecs["count"] . "</b><br/>";

$time = date('H:i \o\n l, jS F Y e');
echo "Актуальность: $time";

// Enumerate internal AD users

if (!($ldaprecs["count"] < 1))
{

//Make table header
echo "<hr>";
echo "<TABLE width=100% align=center  cellpadding=0 cellspacing=0> <tr>";
echo "<td  bgcolor='dfdfdf' ></td>";
echo "<td  bgcolor='dfdfdf' >&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='4'><a href='$pagename?sort_by=telephonenumber'>Внут.</a></font></td>";
echo "<td  bgcolor='dfdfdf' >&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='4'><a href='$pagename?sort_by=cn'>Имя</a></font></td>";
echo "<td  bgcolor='dfdfdf'>&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='4'><a href='$pagename?sort_by=mobile'>Моб.</a></font></td>";
echo "<td  bgcolor='dfdfdf'>&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='4'><a href='$pagename?sort_by=title'>Должность</a></font></td>";
echo "<td  bgcolor='dfdfdf'>&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='4'><a href='$pagename?sort_by=department'>Отдел</a></font></td>";
echo "<td  bgcolor='dfdfdf'>&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='4'><a href='$pagename?sort_by=company'>Компания</a></font></td>";
echo "</tr>";

    for ($i=0; $i < $ldaprecs["count"]; $i++)
        {
    	$cn = $ldaprecs[$i]["cn"][0];
    	        				
    				    $mobile = $ldaprecs[$i]["mobile"][0];
    					
    					    $telephonenumber = $ldaprecs[$i]["telephonenumber"][0];
    						$description = $ldaprecs[$i]["description"][0];
    						    $title = $ldaprecs[$i]["title"][0];
    						    $company = $ldaprecs[$i]["company"][0];
    							$department = $ldaprecs[$i]["department"][0];
    							    $samaccountname = $ldaprecs[$i]["samaccountname"][0];
    								$mail = $ldaprecs[$i]["mail"][0];
    								if($bgcolor=='#f1f1f1'){$bgcolor='#ffffff';}
    								else{$bgcolor='#f1f1f1';}
    								
    								// Trim spaces and shit
    								$title = trim($title);
    								$mobile_clean = preg_replace('%[^0-9]%', '', $mobile);
    								
    								// Do not display those beginning with ~
    								if ((!($cn[0] == "~"))&(!($cn[1] == "_")))
    								{
    								echo "<tr >";
    								echo "<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'><a href=mailto:$mail><img src=envelope.jpg border=0></a></font>";
    								echo "</td>";
    								echo "<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>$telephonenumber</font></td>";
    								echo "<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>$cn</font></td>";
    								echo "<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>$mobile</font></td>";
    								echo "<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>$title</font></td>";
    								echo "<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>$department</font></td>";
    								echo "<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>$company</font></td>";
									echo "</font></td>";
    								echo "</tr>";
    								}
    								    }
    									echo "</table>";
    									}
    									 									    					
    									    					
    									    					ob_end_flush();
    									    					ldap_unbind($ldapconn);
    									    					
    									    					?>